import '../public.dart';

class Validate {
  static Future<bool> validator(Map form, Map rules) async {
    form.forEach((k, v) {
      if (rules.containsKey(k)) {
        var list = rules[k];
        for (var j in list) {
          validatorFnc(form[k], j);
        }
      }
    });
    return true;
  }

  static bool validatorFnc(value, Map rule) {
    if (rule.containsKey("required")) {
      if (ObjectUtil.isEmpty(value)) {
        throw ('${rule['msg']}');
      }
    }

    if (rule.containsKey("reg")) {
      var regexp = RegExp(rule['reg']);
      if (!regexp.hasMatch(value)) {
        throw ('${rule['msg']}');
      }
    }
    return true;
  }
}
